package com.baicun.wish.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.baicun.wish.entity.College;
import com.baicun.wish.entity.WishRecommend;
import com.baicun.wish.entity.dto.Result;
import com.baicun.wish.service.CollegeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.SoundbankResource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    /**
     * 根据collegeConditionFrom 分页查询college 的信息
     * @param jsonObject
     * @return
     */
    @PostMapping("/getCollegeByPage")
    public Result getCollegeByPage(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        JSONObject recommendConditionForm = jsonObject.getJSONObject("collegeConditionFrom");
        String collegeName = recommendConditionForm.getStr("collegeName");
        String province = recommendConditionForm.getStr("province");
        String startType = recommendConditionForm.getStr("startType");
        String collegeType = recommendConditionForm.getStr("collegeType");
        String collegeTag = recommendConditionForm.getStr("collegeTag");
        QueryWrapper<College> collegeQueryWrapper = new QueryWrapper<College>();
        Page page = new Page(currentPage,10);
        if (collegeName != null) {
            collegeQueryWrapper = collegeQueryWrapper.like("college_name",collegeName);
        }
        if (!"全部".equals(province)){
            collegeQueryWrapper = collegeQueryWrapper.eq("province",province);
        }
        if (!"全部".equals(startType)){
            collegeQueryWrapper = collegeQueryWrapper.eq("start_type",startType);
        }
        if (!"全部".equals(collegeType)){
            collegeQueryWrapper = collegeQueryWrapper.like("college_type",collegeType);
        }
        if (!"全部".equals(collegeTag)){
            collegeQueryWrapper = collegeQueryWrapper.like("college_tag",collegeTag);
        }
        IPage<College> iPage = collegeService.page(page,collegeQueryWrapper.orderByDesc("popularity_value"));

        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"查询学校列表为空",iPage);
        }else{
            return Result.success(iPage);
        }
    }


}
